package com.iwhalecloud.bss.kite.cucc.listener.prod.local;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.event.prod.InitAccessProdEvent;
import com.iwhalecloud.bss.kite.listener.handler.detail.impl.MktHandler;
import com.iwhalecloud.bss.kite.listener.handler.detail.impl.ProdInstFuncOfProductHandler;
import com.iwhalecloud.bss.kite.listener.handler.mkt.impl.GiftMktResHandler;
import com.iwhalecloud.bss.kite.listener.handler.offer.impl.AdditionalOfferOfProductHandler;
import com.iwhalecloud.bss.kite.listener.handler.prod.impl.ProdInstRelHandler;
import com.iwhalecloud.bss.kite.listener.handler.prod.impl.ProdRelHandler;
import com.iwhalecloud.bss.kite.listener.prod.IInitAccessProdListener;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
/**
 * @author liu.zhiyong2
 * @date 2019年6月13日
 * @desc
 */

@Service
public class LocalInitAccessProdListener implements IInitAccessProdListener {

    @Autowired
    ProdInstFuncOfProductHandler prodInstFuncOfProductHandler;

    @Autowired
    MktHandler mktHandler;

    @Autowired
    AdditionalOfferOfProductHandler additionalOfferOfProductHandler;

    @Autowired
    ProdRelHandler prodRelHandler;

    @Autowired
    ProdInstRelHandler prodInstRelHandler;

    @Autowired
    GiftMktResHandler giftMktResHandler;
    
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    
    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    /**
     * 新增功能产品，生成属性组实例ID
     *
     * @param event
     */
    @EventListener
    @Order(1)
    @Async
    public void generateAttrGroupInstId(InitAccessProdEvent event) {
        String prodInstId = event.getAccessProdInstId();
        String sceneInstId = event.getSceneInstId();
        String custId = event.getCustId();
        
        KiteSceneInst kiteSceneInst = this.querySceneInstService.getSceneInst(sceneInstId);
        String askSource = kiteSceneInst.getAskSource();
        if (KiteStringUtils.equalsAny(askSource, LocalKeyConsts.ASK_SOURCE_ORDER, LocalKeyConsts.ASK_SOURCE_USER)) {
            return;
        }
        
        CuccContextCustDTO custDTO = new CuccContextCustDTO();
        custDTO.setProvinceCode(kiteSceneInst.getProvinceNbr());
        custDTO.setEparchyCode(kiteSceneInst.getLanId());
        custDTO.setRegionId(kiteSceneInst.getRegionId());

        KiteProdInst prodInst = this.querySceneInstService.getProdInst(custId, prodInstId);
        if (!KiteObjectUtils.isEmpty(prodInst)) {
            /*KiteAttr kiteAttr = new KiteAttr();
            kiteAttr.setFieldName(LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + prodInst.getSpecId());
            kiteAttr.setValue(attrGroupInstId);
            kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            prodInst.setAttr(kiteAttr);
            this.operateSceneInstService.addInst(sceneInstId, prodInst);*/
            
            prodInst.listAttrs().stream().filter(attr -> !KiteStringUtils.isEmpty(attr.getGroupId())).map(KiteAttr::getGroupId).distinct().collect(Collectors.toList()).forEach(groupId -> {
                KiteAttr kiteAttr = new KiteAttr();
                kiteAttr.setFieldName(LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + groupId);
                kiteAttr.setValue(this.getAttrGroupInstId(sceneInstId, prodInstId, custDTO));
                kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                prodInst.setAttr(kiteAttr);
                this.operateSceneInstService.addInst(sceneInstId, prodInst);
            });
            
        }
    }
    
    private String getAttrGroupInstId(String sceneInstId, String prodInstId, CuccContextCustDTO custDTO) {
        // 生成新的属性组实例ID
        Map<String, String> inParam = new HashMap<>();
        inParam.put("SEQ_TYPE", "SERVICE_ATTR_GROUP_ITEM_ID");
        inParam.put("SYS_TYPE", "1");
        inParam.put("SYS_SOURCE", "");
        return LocalQuerySeqHelper.getSeqBySeqType(inParam, sceneInstId, prodInstId, custDTO);
    }
}
